<?xml version="1.0" encoding="utf-8"?>
<!--
  Newsletter XSLT file
  ====================
  This is designed to be a fairly straightforward transformation of a series of source
  XML files into a standardized newsletter. It reads in a table of contents file which
  specifies the stories and headings; these are stored as separate XML files. An example
  toc file would be

    <toc xmlns="urn:csgso:toc">
      <date>November 17, 2004</date>
      <volume>5</volume>
      <issue>2</issue>

      <item name="Students in the department">
        <item file="homecoming.xml" />
        <item file="interview.xml" />
      </item>
     <item name="Seminars and get-togethers">
       <item file="seminars.xml" />
       <item file="muslim.xml" />
       <item file="dinner.xml" />	
     </item>

     <item file="fellowships.xml" />
     <item file="ita.xml" />
   
     <item file="qualifying.xml" />
   
     <item file="movie.xml" />   	
   </toc>

  Each xml file should be in xhtml, wrapped within a few elements in
  the urn:csgso:story namespace. An example should show the requirements:

   <story:story xmlns:story="urn:csgso:story" xmlns="http://www.w3.org/1999/xhtml">
     <story:title>Message from Homecoming King Cameron Mott</story:title>
     <story:body><p>Hello, readers of this sample document.</p></story:body>
   </story:story>

  The XSLT file was designed to be independent of the
  newsletter. Unfortunately, it looks like the original one had some
  unstated dependencies upon a CSS file that set up columns; that CSS
  file is not currently available. 

  It was originally developed for the xalan-j xslt processor but has
  been lightly tested with the 'xsltproc' command-line tool, which is
  installed by COG on the NMSU CS linux systems. In theory, the .toc file
  could directly be exposed with an ?xml-stylesheet processing directive; in 
  practice, client-side XSLT is not very well supported.
-->
<xsl:stylesheet 
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:math="http://exslt.org/math"
  xmlns:toc="urn:csgso:toc"
  xmlns:news="urn:csgso:story"
  exclude-result-prefixes="news toc math xsl"
  version="1.0">
  
  <xsl:output method="xml"/> 
  
  <xsl:template match="toc:toc"> 
    <xsl:variable name="volume" select="toc:volume/text()" />
    <xsl:variable name="date"   select="toc:date/text()" />
    <xsl:variable name="issue"  select="toc:issue/text()" />

    <html>
      <head>
	<title>Graduate Student Newsletter vol <xsl:value-of select="$volume" /> issue <xsl:value-of select="$issue" /></title>
	<style type="text/css" media="all">
	  body { background-color: white; color: black; font-family: serif; }
	  h1, h2, h3, h4, h5, h6 { font-family: sans-serif; }
	  table#heading { width: 100%; } // The banner image should go across the entire page
	</style>
      </head>
 
      <body> 
        <table id="heading">
          <tr><td colspan="2"><center><img src="http://www.cs.nmsu.edu/~gradrep/newsletters/Header.gif" height="84" width="555" alt="The Graduate News header" /></center></td></tr>
          <tr><td><xsl:value-of select="$date" /></td><td style="text-align: right;">Volume <xsl:value-of select="$volume" /> Issue <xsl:value-of select="toc:issue" /></td></tr>
        </table>

        <h1 id="toc">In this issue</h1>
        
        <ul>
          <xsl:apply-templates select="toc:item" mode="title" />
        </ul>
        
        <hr />
        <xsl:apply-templates select="toc:item" />
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="toc:item" mode="title">
    <li>
      <a href="#{generate-id(.)}">
        <xsl:choose>
          <xsl:when test="@name"><xsl:apply-templates select="@name" /></xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="document(@file)" mode="title" />
          </xsl:otherwise>
        </xsl:choose>
      </a>
      <xsl:if test="toc:item">
        <!-- children -->
        <ul>
          <xsl:apply-templates select="toc:item" mode="title" />
        </ul>
      </xsl:if>
    </li>    
  </xsl:template>
  
  <xsl:template match="toc:item">
    <xsl:variable name="level" select="count(ancestor::toc:item)+2" />       
    
    <xsl:element name="h{$level}">
      <a name="{generate-id(.)}">
        <xsl:choose>
          <xsl:when test="@name">
            <xsl:apply-templates select="@name" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="document(@file)" mode="title"/>
          </xsl:otherwise>
        </xsl:choose>
      </a>
    </xsl:element>    
    
    <xsl:if test="@file"><xsl:apply-templates select="document(@file)" /></xsl:if>
    <xsl:apply-templates select="toc:item" />

    <!--<xsl:if test="following-sibling::toc:item"><hr /></xsl:if> -->
  </xsl:template>

  <xsl:template match="news:story" mode="title">
    <xsl:apply-templates select="news:title" />
  </xsl:template>

  <xsl:template match="news:story">    
    <xsl:copy-of select="news:body/*|news:body/text()" />
  </xsl:template>
</xsl:stylesheet>
